//
//  TestDownViewController.m
//  VPPublicUntilitisForPod
//
//  Created by 彭钰炜 on 2017/4/7.
//  Copyright © 2017年 vernepung. All rights reserved.
//
#import "AppDelegate.h"
#import "TestDownViewController.h"
NSString *const downloadUrl = @"http://xz.cr173.com/soft2/sqlite_professional_mac.zip";
NSString *const download1Url = @"http://qzonestyle.gtimg.cn/qzone/vas/opensns/res/doc/iOS_SDK_V3.2.0.lite.zip";
@interface TestDownViewController ()<NSURLSessionDownloadDelegate>
@property (strong, nonatomic) NSURLSessionDownloadTask *downloadTask;
@property (strong, nonatomic) NSURLSessionConfiguration *currentConfig;
@end

@implementation TestDownViewController

- (void)dealloc {
    NSLog(@"TestDownViewController dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:downloadUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    self.currentConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:downloadUrl];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:self.currentConfig delegate:self delegateQueue:nil];
    self.downloadTask = [session downloadTaskWithRequest:mutableRequest];
    [self.downloadTask resume];
    mutableRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:download1Url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    self.currentConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:download1Url];
    session = [NSURLSession sessionWithConfiguration:self.currentConfig delegate:self delegateQueue:nil];
    self.downloadTask = [session downloadTaskWithRequest:mutableRequest];
    [self.downloadTask resume];
    // Do any additional setup after loading the view.
}

#pragma mark - NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location {
    
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
      didWriteData:(int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
    NSLog(@"%lld-%lld",totalBytesWritten,totalBytesExpectedToWrite);
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
 didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes {
    
}

- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error {
    
}

- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
    AppDelegate *appDelegate = [AppDelegate shareAppDelegate];
    if (appDelegate.backgroundSessionCompletionHandler) {
        void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
        appDelegate.backgroundSessionCompletionHandler = nil;
        completionHandler();
    }
    NSLog(@"All tasks are finished");
    
    // 1.创建一个本地通知
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    // 1.2.设置通知内容
    localNote.alertBody = @"下载完成";
    // 1.3.设置锁屏时,字体下方显示的一个文字
    localNote.alertAction = @"赶紧!!!!!";
    localNote.hasAction = YES;
    
    // 1.5.设置通过到来的声音
    localNote.soundName = UILocalNotificationDefaultSoundName;
    
    // 1.6.设置应用图标左上角显示的数字
    localNote.applicationIconBadgeNumber = 1;
    // 2.执行通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
